package src.cs2340.todoApp;

/**
 * Date.java
 * @author Kristian
 * @version 1.10 March 9, 2012
 * Will create specifications for the time in TaskItem.java.  The time there has
 * hours, minutes, a day, a month, and a year.
 */
public class Date implements Comparable<Date>
{
	public enum Meredian {
		AM,PM;
	}
	private int day;
	private int month;
	private int year;
	private int hour;
	private int minute;
	private Meredian aP;
	
	/**
	 * Constructor.
	 * @param day The day the TaskItem is due.
	 * @param month The month the TaskItem is due.
	 * @param year The year the TaskItem is due.
	 * @param hour The hour the TaskItem is due.
	 * @param min The minute the TaskItem is due.
	 */
	public Date(int day, int month, int year, int hour, int minute, Meredian aP)
	{
		this.day = day;
		this.month = month;
		this.year = year;
		this.hour = hour;
		this.minute = minute;
		this.setaP(aP);
	}
	
	/**
	 * A getter for day.
	 * @return day
	 */
	public int getDay()
	{
		return day;
	}
	
	/**
	 * A setter.  Overwrites the int held by Date for day.
	 * @param day The specified int that will be the new day for
	 * 				a particular instance of Date.
	 */
	public void setDay(int day)
	{
		this.day = day;
	}
	
	/**
	 * A getter for month.
	 * @return month
	 */
	public int getMonth()
	{
		return month;
	}
	
	/**
	 * A setter.  Overwrites the int held by Date for month.
	 * @param month The specified int that will be the new month for
	 * 				 a particular instance of Date. 
	 */
	public void setMonth(int month)
	{
		this.month = month;
	}
	
	/**
	 * A getter for year.
	 * @return year
	 */
	public int getYear()
	{
		return year;
	}
	
	/**
	 * A setter. Overwrites the int held by Date for year.
	 * @param year The specified int that will be the new year for
	 * 				a particular instance of Date.
	 */
	public void setYear(int year)
	{
		this.year = year;
	}
	
	/**
	 * A getter for hour.
	 * @return hour
	 */
	public int getHour()
	{
		return hour;
	}
	
	/**
	 * A setter.  Overwrites the int held by Date for hour.
	 * @param hour The specified int that will be the new hour value for a 
	 * 				particular instance of Date.
	 */
	public void setHour(int hour)
	{
		this.hour = hour;
	}
	
	
	/**
	 * A getter for minute.
	 * @return minute
	 */
	public int getMinute()
	{
		return minute;
	}
	
	/**
	 * A setter.  Overwrites the int held by Date for minute.
	 * @param min The specified int that will be the new minute value for a 
	 * 				particular instance of Date.
	 */
	public void setMinute(int min)
	{
		minute = min;
	}

	/**
	 * @param aP the aP to set
	 */
	public void setaP(Meredian aP) {
		this.aP = aP;
	}

	/**
	 * @return the aP
	 */
	public Meredian getaP() {
		return aP;
	}
	
	/**
	 * toString() method for Date
	 */
	@Override
	public String toString(){
		String hrmn = "";
		if (hour == 0 && minute == 0) {
			hrmn = "12:00";
		} else if (hour == 0) {
			hrmn = "12:" + (minute > 10 ? minute : ("0" + minute));
		} else if (minute == 0) {
			hrmn = (hour > 10 ? hour : ("0" + hour)) + ":00";
		} else {
			hrmn = (hour > 9 ? hour : ("0" + hour)) + ":" + (minute > 10 ? minute : ("0" + minute));
		}
		return (this.month < 10 ? ("0" + this.month) : this.month) + "/" 
		       + (this.day < 10 ? ("0" + this.day) : this.day) + "/" 
		       + year + " " + hrmn + " " + aP;
	}
	
	/**
	 * Compares two Dates by comparing year and then concatanating the other 
	 * date variables and subtracting.
	 */
	@Override
	public int compareTo(Date d) {
		int diff = this.year - d.year;
		if(diff == 0){
			int currentHour = this.aP == Meredian.PM ? (12 + this.hour) : this.hour;
			String s1 = (this.month < 10 ? ("0" + this.month) : this.month) + "" 
					  + (this.day < 10 ? ("0" + this.day) : this.day) + "" 
					  + (currentHour < 10 ? ("0" + currentHour) : currentHour) + "" 
					  + (this.minute < 10 ? ("0" + this.minute) : this.minute);
			currentHour = d.aP == Meredian.PM ? (12 + d.hour) : d.hour;
			String s2 = (d.month < 10 ? ("0" + d.month) : d.month) + "" 
			  + (d.day < 10 ? ("0" + d.day) : d.day) + "" 
			  + (currentHour < 10 ? ("0" + currentHour) : currentHour) + "" 
			  + (d.minute < 10 ? ("0" + d.minute) : d.minute);		
			return Integer.parseInt(s1) - Integer.parseInt(s2);
		}
		return diff;
	}
}
